| Conditions | 1 |
| Paths | 1 |
| Total Lines | 141 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | 'use strict'; |
||
| 10 | function (state, data, visibility, util, format, reaction) { |
||
| 11 | let ct = this; |
||
| 12 | ct.state = state; |
||
| 13 | ct.data = data; |
||
| 14 | ct.visibility = visibility; |
||
| 15 | ct.util = util; |
||
| 16 | ct.format = format; |
||
| 17 | ct.reaction = reaction; |
||
| 18 | |||
| 19 | ct.channels = 2; |
||
| 20 | ct.bandwidth = 100; |
||
| 21 | |||
| 22 | function update(player) { |
||
| 23 | for (let redox of ct.state.player.redox) { |
||
| 24 | if (!redox.resource || !redox.active) { |
||
| 25 | continue; |
||
| 26 | } |
||
| 27 | |||
| 28 | let reactant = ct.generateName(redox.resource, redox.from); |
||
| 29 | let power = ct.redoxPower(player); |
||
| 30 | let number = Math.min(power, player.resources[reactant].number); |
||
| 31 | let react = ct.redoxReaction(redox); |
||
| 32 | |||
| 33 | ct.reaction.react(number, react, player); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | ct.redoxPower = function(player) { |
||
| 38 | let level = player.global_upgrades.redox_bandwidth; |
||
| 39 | let upgrade = data.global_upgrades.redox_bandwidth; |
||
| 40 | let basePower = upgrade.power; |
||
| 41 | let polynomial = upgrade.power_poly; |
||
| 42 | return basePower * Math.floor(Math.pow(level, polynomial)); |
||
| 43 | } |
||
| 44 | |||
| 45 | ct.redoxReaction = function (redox) { |
||
| 46 | let reactant = ct.generateName(redox.resource, redox.from); |
||
| 47 | let product = ct.generateName(redox.resource, redox.to); |
||
| 48 | // we retrieve the element of the isotope from the resource |
||
| 49 | let element = Object.keys(data.resources[redox.resource].elements)[0]; |
||
| 50 | let energy = redoxEnergy(redox.from, redox.to, element); |
||
| 51 | |||
| 52 | let react = { |
||
| 53 | 'reactant': {}, |
||
| 54 | 'product': {} |
||
| 55 | }; |
||
| 56 | |||
| 57 | react.reactant[reactant] = 1; |
||
| 58 | react.product[product] = 1; |
||
| 59 | if (energy > 0) { |
||
| 60 | react.reactant.eV = energy; |
||
| 61 | } else if (energy < 0) { |
||
| 62 | react.product.eV = -energy; |
||
| 63 | } |
||
| 64 | |||
| 65 | let electron = redox.from - redox.to; |
||
| 66 | if (electron > 0) { |
||
| 67 | react.reactant['e-'] = electron; |
||
| 68 | } else if (electron < 0) { |
||
| 69 | react.product['e-'] = -electron; |
||
| 70 | } |
||
| 71 | |||
| 72 | return react; |
||
| 73 | }; |
||
| 74 | |||
| 75 | function redoxEnergy(from, to, element) { |
||
| 76 | let energyFrom = cumulativeEnergy(element, from); |
||
| 77 | let energyTo = cumulativeEnergy(element, to); |
||
| 78 | let energy = energyTo - energyFrom; |
||
| 79 | |||
| 80 | return energy; |
||
| 81 | } |
||
| 82 | |||
| 83 | function cumulativeEnergy(element, level) { |
||
| 84 | let energy = 0; |
||
| 85 | let start = Math.min(0, level); |
||
| 86 | let end = Math.max(0, level); |
||
| 87 | for (let i = start; i <= end; i++) { |
||
| 88 | energy += data.redox[element][i]; |
||
| 89 | } |
||
| 90 | if (level < 0) { |
||
| 91 | energy = -energy; |
||
| 92 | } |
||
| 93 | return energy; |
||
| 94 | } |
||
| 95 | |||
| 96 | // Generates the name of a ion, e.g. 18O3+ |
||
| 97 | ct.generateName = function (isotope, i) { |
||
| 98 | if (i === 0) { |
||
| 99 | return isotope; |
||
| 100 | } |
||
| 101 | let postfix = ''; |
||
| 102 | if (Math.abs(i) > 1) { |
||
| 103 | postfix = Math.abs(i); |
||
| 104 | } |
||
| 105 | postfix += getSign(i); |
||
| 106 | let name = isotope + postfix; |
||
| 107 | // special case!! 1H+ is just a proton |
||
| 108 | if (name === '1H+') { |
||
| 109 | name = 'p'; |
||
| 110 | } |
||
| 111 | return name; |
||
| 112 | }; |
||
| 113 | |||
| 114 | function getSign(number) { |
||
| 115 | return number > 0 ? '+' : '-'; |
||
| 116 | } |
||
| 117 | |||
| 118 | ct.redoxSlots = function (player) { |
||
| 119 | let level = player.global_upgrades.redox_slots; |
||
| 120 | let upgrade = data.global_upgrades.redox_slots; |
||
| 121 | let basePower = upgrade.power; |
||
| 122 | let multiplier = upgrade.power_mult; |
||
| 123 | return basePower * Math.floor(multiplier * level); |
||
| 124 | |||
| 125 | }; |
||
| 126 | |||
| 127 | ct.redoxSize = function (player) { |
||
| 128 | return player.redox.length; |
||
| 129 | } |
||
| 130 | |||
| 131 | ct.addRedox = function (player) { |
||
| 132 | if(ct.redoxSize(player) >= ct.redoxSlots(player)){ |
||
| 133 | return; |
||
| 134 | } |
||
| 135 | player.redox.push({ |
||
| 136 | resource: data.elements[ct.state.currentElement].main, |
||
| 137 | number: 50, |
||
| 138 | active: false, |
||
| 139 | element: ct.state.currentElement, |
||
| 140 | from: 0, |
||
| 141 | to: 1 |
||
| 142 | }); |
||
| 143 | }; |
||
| 144 | |||
| 145 | ct.removeRedox = function (player, index) { |
||
| 146 | player.redox.splice(index, 1); |
||
| 147 | }; |
||
| 148 | |||
| 149 | state.registerUpdate('redox', update); |
||
| 150 | } |
||
| 151 | ]); |
||
| 152 |